/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package uy.edu.ort.CentroEstudios.servicios;

import java.util.Collection;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceException;
import uy.edu.ort.CentroEstudios.entidades.Docente;
import uy.edu.ort.CentroEstudios.entidades.Grupo;
import uy.edu.ort.CentroEstudios.entidades.Materia;

/**
 *
 * @author scirillo
 */
@Stateless
public class ConsultarDocenteEJB implements DocenteEJBLocal {
    
    @PersistenceContext
    private EntityManager entityManager;

    @Override
    public Docente login(Docente u) {
       
        String jpql = "Select d from Docente d where nombre = '"+u.getNombreDocente()+"' and password = '"+
                u.getPassword()+"' and tipo = "+u.getTipo()+"";
        Docente docente = (Docente)entityManager.createQuery(jpql).getSingleResult();
        if(docente != null){
            return docente;
        }
        return null;
    }

    // Add business logic below. (Right-click in editor and choose
    // "Insert Code > Add Business Method")

    @Override
    public void asignarContraseña(Collection<Docente> docentes) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void pasajeLista() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void guardarDocente(Docente d) {
        try {
            Docente doc = entityManager.find(Docente.class, d.getIdDocente());
            if(doc == null){
                entityManager.persist(d);
                entityManager.flush();
            }
            
        } catch (PersistenceException pe) {
            Logger.getLogger(Grupo.class.getName()).log(Level.WARNING, "Error al persistir el docente: " + pe.getMessage(), pe);
        }
    }

    @Override
    public void guardarMaterias(Materia m) {
         try {
            entityManager.persist(m);
            entityManager.flush();
        } catch (PersistenceException pe) {
            Logger.getLogger(Grupo.class.getName()).log(Level.WARNING, "Error al persistir la materia: " + pe.getMessage(), pe);
        }
    }
}
